package client.core;

import java.util.Vector;

import entity.Reply;
import entity.Request;
import entity.TextInfo;
import entity.UserInfo;

public class ClientCore {
	
	SyncTransfer st;

	public ClientCore(SyncTransfer st) {
		this.st = st;
	}

	/**
	 * 
	 * @param _name
	 *            用户名
	 * @param _pwd
	 *            密码
	 * @param _nickname
	 *            昵称
	 * @param _school
	 *            学校
	 * @return
	 */
	public boolean register(String _name, String _pwd, String _nickname,
			String _school) {

		Request req = new Request(0);
		UserInfo usrinfo = new UserInfo();
		usrinfo.setName(_name);
		usrinfo.setPassword(_pwd);
		usrinfo.setNickname(_nickname);
		usrinfo.setSchool(_school);
		req.setRegister_usrinfo(usrinfo);
		
		Reply rep = st.transfer(req);
		
		if (rep == null)
			return false;
		
		if (rep.getType() == -1) {
			return false;
		} else {
			return rep.isRegisterSuccessful();
		}

	}

	/**
	 * 
	 * @param _name
	 *            登录用户名
	 * @param _pwd
	 *            密码
	 * @return
	 */
	public UserInfo login(String _name, String _pwd) {

		Request req = new Request(1);
		req.setLogin_name(_name);
		req.setLogin_pwd(_pwd);
		
		Reply rep = st.transfer(req);
		if (rep == null)
			return null;
		
		if (rep.getType() == -1) {
			return null;
		} else {
			if (rep.isLoginSuccessful()) {
				return rep.getLogin_usrinfo();
			} else {
				return null;
			}
		}

	}

	/**
	 * 
	 * @param _uid
	 *            发起查询操作的用户ID
	 * @param _nickname
	 *            被查询用户的昵称
	 * @return
	 */
	public Vector<UserInfo> searchUser(long _uid, String _nickname) {

		Request req = new Request(2);
		req.setSearchUser_s_uid(_uid);
		req.setSearchUser_t_nickname(_nickname);
		
		Reply rep = st.transfer(req);
		if (rep == null)
			return null;
		
		if (rep.getType() == -1) {
			return null;
		} else {
			return rep.getSearchUser_vui();
		}

	}

	/**
	 * 
	 * @param _uid
	 *            发起添加好友请求的用户ID
	 * @param _name
	 *            被添加好友的用户ID
	 * @return
	 */
	public boolean addFriend(long _s_uid, long _t_uid) {

		Request req = new Request(3);
		req.setAddFriend_s_uid(_s_uid);
		req.setAddFriend_t_uid(_t_uid);

		Reply rep = st.transfer(req);
		if (rep == null)
			return false;
		
		if (rep.getType() == -1) {
			return false;
		} else {
			return rep.isAddFriendSuccessful();
		}

	}

	/**
	 * 
	 * @param _uid
	 *            发布新鲜事的用户ID
	 * @param _text
	 *            新鲜事内容
	 * @return
	 */
	public boolean publishNewsFeed(long _uid, String _text, int _type) {

		Request req = new Request(4);
		TextInfo txtinfo = new TextInfo();
		txtinfo.setUID(_uid);
		txtinfo.setText(_text);
		txtinfo.setType(_type);
		req.setTxtinfo(txtinfo);

		Reply rep = st.transfer(req);
		if (rep == null)
			return true;
		
		if (rep.getType() == -1) {
			return true;
		} else {
			return rep.isPublishNewsFeedSuccessful();
		}
		
	}

	/**
	 * 
	 * @param _uid
	 *            发起查看新鲜事的用户ID
	 * @return
	 */
	public Vector<TextInfo> checkNewsFeed(long _uid) {

		Request req = new Request(5);
		req.setCheckNewsFeed_uid(_uid);

		Reply rep = st.transfer(req);
		if (rep == null)
			return null;
		
		if (rep.getType() == -1) {
			return null;
		} else {
			return rep.getCheckNewsFeed_vti();
		}

	}

}
